package com.ruoyi.common.utils;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import com.ruoyi.common.utils.spring.SpringUtils;
import org.springframework.core.env.Environment;

/**
 * @author yaowk
 **/
public class SnowflakeUtils {

    private static Snowflake snowflake;

    public static Snowflake getSnowflake() {
        if (snowflake == null) {
            snowflake = IdUtil.getSnowflake(RandomUtil.randomLong(0, 31), Long.parseLong(SpringUtils.getBean(Environment.class).getProperty("server.port")) % 31);
        }
        return snowflake;
    }
}
